#include <stdio.h>
#include <time.h>
#include <string.h>
#include <dirent.h>
#include "cgic.h"
char *NamePly[1000];
int Plyindex[1000];
int maxPly=0;
int readFileList();
int cgiMain(void)
{
		
		int images_count=40;
		int images_index=0;
		time_t now; 
		struct tm *timenow; 
		char strtemp[255];
		time(&now);
		timenow = localtime(&now); 
		printf("Contenttype:text/html\n\n");
		printf("<html>\n");
		printf("<Meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n");
		printf("</head>\n");
		readFileList("/opt/boa/www/appdata/");
		printf("<script languange=\"javascript\">\n");
		printf("function newsnext(path){\n");
		printf("var form1 = document.createElement(\"form\");\n");
		printf("form1.id = \"form1\";  \n");
		printf("form1.name = \"form1\";\n");
		printf("document.body.appendChild(form1);\n");
		printf("var input = document.createElement(\"input\");\n");
		printf("input.type = \"hidden\";\n");
		printf("input.name = \"value1\";\n");
		printf("input.value =path;\n");
		printf("form1.appendChild(input); \n");
		printf("form1.method = \"post\";\n");
		printf("form1.action = \"uploadelst.cgi\"; \n");
		printf("form1.submit();  \n");
		printf("document.body.removeChild(form1);\n");
		printf("alert(\"Send Message:\"+path);\n");
		printf("}\n");
		
		printf("function newlst(){\n");
		printf("var form1 = document.createElement(\"form\");\n");
		printf("form1.id = \"form1\";  \n");
		printf("form1.name = \"form1\";\n");
		printf("document.body.appendChild(form1);\n");
		printf("var input = document.createElement(\"input\");\n");
		printf("input.type = \"hidden\";\n");
		printf("input.name = \"value1\";\n");
		printf("input.value =1;\n");
		printf("form1.appendChild(input); \n");
		printf("form1.method = \"post\";\n");
		printf("form1.action = \"program.cgi\"; \n");
		printf("form1.submit();  \n");
		printf("document.body.removeChild(form1);\n");
		printf("}\n");
		
		printf("</script>\n");
		
		printf("<body><br>\n");
		printf("<table width=\"100%\" height=\"100px\" board=\"1\"><br>\n");
		printf("<tr>\n");///row ///td cell
		printf("<td width=\"30%\"><input type=image name=\"myimage\"  value=\"all\" src=\"/tools_new_plst.png\" width=\"90%\"  onclick=\"newlst();\"/><br><span style=\"text-align:center;top:0;\">Create playlst</span>\n");
		printf("<td width=\"30%\"><input type=image name=\"myimage\"  value=\"11\" src=\"/tools_uplload_plst.png\" width=\"90%\"  onclick=\"newsnext('%s');\"/><br><span style=\"text-align:center;top:0;\">%s</span>\n",NamePly[0],NamePly[0]);
		printf("<td width=\"30%\"><input type=image name=\"myimage\"  value=\"11\" src=\"/tools_uplload_plst.png\" width=\"90%\"  onclick=\"newsnext('%s');\"/><br><span style=\"text-align:center;top:0;\">%s</span>\n",NamePly[1],NamePly[1]);
		printf("</tr>\n");
	
			//	maxPly Plyindex NamePly
		for ( images_index=2; images_index < maxPly; images_index+=1)
		{
			if((images_index+1)%3==0)
				{
					printf("<tr>\n");
				}
				printf("<td width=\"30%\"><input type=image name=\"myimage\"  value=\"11\" src=\"/tools_uplload_plst.png\" width=\"90%\"  onclick=\"newsnext('%s');\"/><br><span style=\"text-align:center;top:0;\">%s</span>\n",NamePly[images_index],NamePly[images_index]);
				if((images_index+1)%3==0)
				{
					printf("</tr>\n");
				}
		}
		printf("</table>\n");
	//	printf("</form>\n");
		printf("</body>\n");
		printf("</html>\n");
		fflush(stdout);
}
int readFileList(char *basePath)
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];
		char nb[10];
		
		int i=0;
    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        return 1;
      //  exit(1);
    }
    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///扫描当前路径
            continue;
        else if(ptr->d_type == 8)///file 只获取文件类型，文件夹不获取
         if(strstr(ptr->d_name,".lst")>2)//从文件系列中过滤其他类型的文件，此处只需要lst
         	{
         		 NamePly[i++]=ptr->d_name;
         		 maxPly=i;
         	}
         
    }
    closedir(dir);
    return 1;
}